package com.qkl.xiatang.app.dto.message;

import com.alibaba.fastjson.JSON;
import com.qkl.xiatang.app.dto.feed.FeedVO;
import com.qkl.xiatang.app.dto.pond.QueryPondNamesOutDTO;
import com.qkl.xiatang.app.entity.feed.FeedCommonEntity;
import com.qkl.xiatang.app.entity.message.MessageEntity;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.Constants;
import com.qkl.xiatang.app.utils.MsgUtils;
import com.qkl.xiatang.app.utils.appenum.MessageTypeEnum;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Data
public class MessageSendVO {

    private String companyId;

    private String title;

    private String titleSub;

    private String message;

    private String message2;

    private String message3;

    private String message4;

    private String content;

    private List<String> imageList;

    private MessageTypeEnum type;

    private Integer subType;

    private Integer threeType;

    private Date msgDate;

    private List<MessageFormatDTO> detailList;

    private List<String> pondIdList;

    private void addDetailList(MessageFormatDTO formatDTO){
        if(ObjectUtils.isEmpty(detailList)){
            detailList = new ArrayList<>();
        }
        detailList.add(formatDTO);
    }

    public void addDetailList(List<String> pondNames){
        this.addDetailList("",pondNames,null);
    }
    public void addDetailList(String userName,List<String> pondNames,List<FeedMsgDTO> feed){
        MessageFormatDTO formatDTO = new MessageFormatDTO();
        formatDTO.setPersonnel(new ArrayList<>());
        if(StringUtils.isNotEmpty(userName) || ObjectUtils.isNotEmpty(pondNames)) {
            formatDTO.getPersonnel().add(PersonnelDTO.create(userName, MsgUtils.formatPondNames(pondNames)));
        }
        formatDTO.setFeed(feed);
        if(ObjectUtils.isEmpty(formatDTO.getFeed())){
            formatDTO.setFeed(new ArrayList<>());
        }
        this.addDetailList(formatDTO);
    }

    public void addDetailList(List<QueryPondNamesOutDTO> pondNamesList, FeedVO feedVO){
        List<FeedMsgDTO> feedMsgList = feedVO.getFeedMsgList();
        Map<String,List<PersonnelDTO>> mapPersonnel = new LinkedHashMap<>();
        this.pondIdList = new ArrayList<>();
        for (QueryPondNamesOutDTO dto : pondNamesList){
            pondIdList.addAll(dto.getPondIdSet());
            String key = "";
            if(ObjectUtils.isNotEmpty(dto.getFeedIdSet())) {
                Set<String> feedIdSet = new TreeSet<>();
                feedIdSet.addAll(dto.getFeedIdSet());
                key = String.join(",", feedIdSet);
            }
            if( !mapPersonnel.containsKey(key)){
                mapPersonnel.put(key,new ArrayList<>());
            }
            mapPersonnel.get(key).add(PersonnelDTO.create(dto.getUserName(),dto.getPondNames()));
        }
        Set<Map.Entry<String,List<PersonnelDTO>>> setPersonnel = mapPersonnel.entrySet();
        for(Map.Entry<String,List<PersonnelDTO>> entry : setPersonnel){
            String key = entry.getKey();
            List<String> feedIdList = AppUtils.stringToList(key);
            List<FeedMsgDTO> feed = new ArrayList<>();
            for (FeedMsgDTO feedMsgDTO : feedMsgList){
                if(feedIdList.contains(feedMsgDTO.getFeedId())){
                    feed.add(feedMsgDTO);
                }
            }
            MessageFormatDTO formatDTO = new MessageFormatDTO();
            formatDTO.setFeed(feed);
            formatDTO.setPersonnel(entry.getValue());
            this.addDetailList(formatDTO);
        }

    }

    public void addDetailList(FeedCommonEntity feedEntity){
        FeedMsgDTO msgDTO = new FeedMsgDTO();
        msgDTO.setFeedId(feedEntity.getId());
        msgDTO.setFeedName(feedEntity.getName());
        msgDTO.setImg(feedEntity.getImage());
        List<FeedMsgDTO> feed = new ArrayList<>();
        feed.add(msgDTO);
        this.addDetailList("",null,feed);
    }

    public MessageEntity toMessageEntity(){
        String contentDetail = appendContent(titleSub,message);
        contentDetail = appendContent(contentDetail,message2);
        contentDetail = appendContent(contentDetail,message3);
        contentDetail = appendContent(contentDetail,message4);
        contentDetail = appendContent(contentDetail,content);

        String messageId = AppUtils.genIdStr();
        MessageEntity entity = new MessageEntity();
        entity.setId(messageId);
        entity.setCompanyId(companyId);
        entity.setType(type.getType());
        entity.setSubType(subType);
        entity.setThreeType(threeType);
        entity.setTitle(title);
        entity.setTitleSub(titleSub);
        entity.setContent(contentDetail);
        entity.setMsgDate(msgDate);
        entity.setMessage(message);
        entity.setMessage2(message2);
        entity.setMessage3(message3);
        entity.setMessage4(message4);
        if(ObjectUtils.isNotEmpty(imageList)){
            entity.setImage(imageList.get(0));
            entity.setImageList(String.join(",",imageList));
        }
        if(ObjectUtils.isNotEmpty(detailList)){
            entity.setMessageDetail(JSON.toJSONString(detailList));
        }
        entity.setPondIdList(pondIdList);
        return entity;
    }

    private static String appendContent(String content,String append){
        if(StringUtils.isAllEmpty(content,append)){
            return "";
        }
        if(StringUtils.isNotEmpty(content) && StringUtils.isNotEmpty(append)){
            return content + Constants.ENTER_LINE + append;
        }
        return content + StringUtils.trimToEmpty(append);
    }

    public static MessageSendVO create(String companyId, String title, String titleSub, MessageTypeEnum type, Integer subType, Date msgDate, String content){
        return  create(companyId,title,titleSub,null,null,null,null,content,null,type,subType,msgDate,null);
    }

    private static MessageSendVO create(String companyId, String title, String titleSub, String message, String message2, String message3, String message4, String content, List<String> imageList, MessageTypeEnum typeEnum, Integer subType, Date msgDate, List<MessageFormatDTO> detailList){
        MessageSendVO dto = new MessageSendVO();
        dto.setCompanyId(companyId);
        dto.setContent(content);
        dto.setTitle(title);
        dto.setType(typeEnum);
        dto.setTitleSub(titleSub);
        dto.setMsgDate(msgDate);
        dto.setSubType(subType);
        dto.setMessage(message);
        dto.setMessage2(message2);
        dto.setMessage3(message3);
        dto.setMessage4(message4);
        dto.setDetailList(detailList);
        dto.setImageList(imageList);
        return dto;
    }
}
